var basePage = require('../../common/BasePage');
Page({
	behaviors: [basePage],
	data: {
        checkMobile: false,
        checkwxAuth:false,
		goodsImgArr: [],
		active: 0,
		toView: "detail",
		minHeight: "100vh",
		goodsTopNum: 0,
		flowerDetailsNum: 0,
		evaluateNum: 0,
		storeinfoNum: 0,
		noteNum: 0,
		pid: 0,
		goodsInfo: "",
		article_content: "",
		supplier_id: 0,
		commentList: [],
		title: "加入购物车",
		addCart: false,
		buyType: "buy",
		nums: 1,
		uid: "",
		goodsType: "flower",
		whType:"flower",
		shoucang: 0,
		pageTabs: [
		
			{
				id: 'flowerDetails',
				num: 0
			},
			{
				id: 'evaluate',
				num: 0
			},
			{
				id: 'storeinfo',
				num: 0
			},
			{
				id: 'note',
				num: 0
			},
			/**只做判断 */
			{
				id: 'max',
				num: 0
			},
		],
		wh_id:1

	},
	onLoad: function (options) {
		console.log("options",options)
		if (options.uid) {
			this.setData({
				uid: options.uid
			})
		}
		this.setData({
			pid: options.pid,
			supplier_id: options.supplier_id,
			goodsType: options.from?options.from:"flower",
			whType:options.whType,
			wh_id:options.wh_id?options.wh_id:getApp().globalData.warehouse.wh_id
		})
		this.getDetail()
		this.getComment()

		this.request({
			url: this.data.host + '/home/article/detail?article_id=29'
		}, this.get_detail_ok);
		var that = this
		var query = wx.createSelectorQuery();

		let arr0=this.data.pageTabs;
		for (let i = 0; i < this.data.pageTabs.length; i++) {
			let obj = this.data.pageTabs[i];
			query.select('#' + obj.id).boundingClientRect(function (res) {
				// obj.num = res.top;
				arr0[i].num=	res.top;
			}).exec()
		}
		this.setData({
			pageTabs: arr0,
		})
		console.log("arr",this.data.pageTabs);
		query.select('#goodsTop').boundingClientRect(function (res) {
			that.setData({
				goodsTopNum: res.height
			})
		}).exec()
		// //获取板块一离顶部的距离
		//  query.select('#flowerDetails').boundingClientRect(function (res) {
		// 	that.setData({
		// 		flowerDetailsNum:res.top
		// 	})
		//  }).exec()
		//  query.select('#evaluate').boundingClientRect(function (res) {
		// 	that.setData({
		// 		evaluateNum:res.top
		// 	})
		//  }).exec()
		//  query.select('#storeinfo').boundingClientRect(function (res) {
		// 	that.setData({
		// 		storeinfoNum:res.top
		// 	})
		//  }).exec()
		//  query.select('#note').boundingClientRect(function (res) {
		// 	that.setData({
		// 		noteNum:res.top
		// 	})
		//  }).exec()
	},
	onShow: function () {

	},
	//收藏
	onCollection() {
		let pdc = this.data.goodsInfo;
		let status = this.data.shoucang == 1 ? 0 : 1;
		this.request({
			url: this.data.host + '/home/user/collect/collect',
			data: {
				status: status,
				can_id: pdc.pid,
				type: 1
			},
			method: 'post'
		}, res => {

			this.setData({
				shoucang: status
			})
		});
	},
	//查询商品详情
	getDetail() {
		var url="/home/supplier/product/detail"
		if(this.data.whType=="pindan"){
			url="/home/wh/product/detail?wh_id="+this.data.wh_id
		}

		var data={
			pid: this.data.pid
		}

		// if(this.data.goodsType=="yuyue"){//预约
		// 	url="/home/yuyue/detail"
		// 	data={
		// 		yuyue_id: this.data.pid
		// 	}
		// }
		this.request({
			url: this.data.host + url,
			data: data,
			method: "GET"
		}, res => {
      var goodsImgArr=[]
      if(res.data.have_video==1){
        goodsImgArr.push({
          type:"video",
          thumb:this.data.imgHost+'/'+res.data.video
        })
      }
			res.data.photo_list.map(val => {
        goodsImgArr.push({
          type:"img",
          thumb:this.get_thumb(val.photo, "wide")
        })
				// val.thumb = this.get_thumb(val.photo, "wide");
			})
			res.data.supplier.product_list.map(val => {
				val.thumb = this.get_thumb(val.product_img);
			})
			res.data.supplier.logo = this.get_thumb(res.data.supplier.logo)

			this.setData({
				goodsImgArr: goodsImgArr,
				goodsInfo: res.data,
				shoucang: res.data.collected
			})

		})
	},
	getComment() {
		this.request({
			url: this.data.host + "/home/supplier/comment",
			data: {
				limit: 2,
				pid: this.data.pid,
				supplier_id: this.data.supplier_id
			},
			method: "GET"
		}, res => {
			var list = res.data

			let that = this;

			list.map(val => {
				val.product_thumb = this.get_thumb(val.product_img);
				val.face = this.get_thumb(val.user_face);
				let photoArr = [];
				val.photos.map(photo => {
					photoArr.push(that.get_thumb(photo))
				})
				val.photoArr = photoArr;
				val.videos.map(video => {
					val.video = that.data.imgHost + "/" + video
				})
			})

			this.setData({
				commentList: list
			})
		})
	},
	get_detail_ok(res) {
        var article_content=""
        if(res.data){
            article_content=res.data.article_content.content.replace(/<img /ig, '<img width="100%" ')
        }
		this.setData({
			article_content: article_content
		});
	},
	//跳转到评价
	toevaluat(e) {
		// console.log("评论点击", this.data.pdc);
		let comment = this.data.commentList;
		if (!comment) {
			return wx.showToast({
				title: '该产品暂无评论',
				icon: 'none'
			})
		}
		wx.navigateTo({
			url: '/pages/cls/evaluat/evaluat?supplier_id=' + this.data.supplier_id + "&pid=" + this.data.pid,
		})
	},
	onChange(e) {
		// console.log("tab",e)
		this.setData({
			active: e.currentTarget.dataset.index * 1,
			toView: e.currentTarget.dataset.name
		})
	},
	scrollTo(e) {
		var that = this
		var scrollTop = e.detail.scrollTop + 44
		// var flowerDetailsNum = this.data.flowerDetailsNum //鲜花信息
		// var evaluateNum = this.data.evaluateNum //评价
		// var storeinfoNum = this.data.storeinfoNum //卖家
		// var noteNum = this.data.noteNum //注意事项
		for (let i = 0; i < this.data.pageTabs.length - 1; i++) {
			let obj = this.data.pageTabs[i];
			let obj1 = this.data.pageTabs[i + 1];
			// if(scrollTop >=obj.num)
			
			if (scrollTop >=obj.num && scrollTop < obj1.num) {
				that.setData({
					active: i
				});
				return;
			}
	
			// if(scrollTop>=obj1.num){
			// 	that.setData({
			// 		active:this.data.pageTabs.length - 1
			// 	})
			// }
			
		}
		// if(scrollTop>=flowerDetailsNum&&scrollTop<evaluateNum){
		// 	that.setData({
		// 		active:0
		// 	})
		// }else if(scrollTop>=evaluateNum&&scrollTop<storeinfoNum){
		// 	that.setData({
		// 		active:1
		// 	})
		// 	// console.log("active",that.data.active)
		// }else if(scrollTop>=storeinfoNum&&scrollTop<noteNum){
		// 	that.setData({
		// 		active:2
		// 	})
		// }else if(scrollTop>=noteNum){
			// that.setData({
			// 	active:3
			// })
		// }
	},
	//跳转详情
	toDetail(e) {
		// console.log("e",e)
		wx.navigateTo({
			url: '/pages/goodsDetail/goodsDetail?pid=' + e.currentTarget.dataset.pid + "&supplier_id=" + this.data.supplier_id
		})
	},
	submit_click(e) {
		var title = "";
		if (e.currentTarget.dataset.pay == "buy") {
			title = "立即购买"
		} else {
			title = "加入购物车"
		}
		this.setData({
			title: title,
			buyType: e.currentTarget.dataset.pay,
			addCart: true
		})
	},
	add_cart() {
		if (this.data.buyType == "add") {
			var url="/home/cart/addToCart"
			if(this.data.whType=="pindan"){
				url="/home/wh/cart/addToCart?wh_id="+this.data.wh_id
			}
			this.request({
				url: this.data.host + url,
				data: {
					pid: this.data.pid,
					nums: this.data.nums,
					from: this.data.goodsType
				},
				method: 'post'
			}, res => {
				wx.showToast({
					title: res.msg,
					icon: 'none',
					duration: 2000
				})
			});
		}else if(this.data.buyType == "buy"){
			var url="/home/flower/preInfo"
			if(this.data.whType=="pindan"){
				url="/home/wh/order/preInfo"
			}
			this.request({
				url: this.data.host + url,
				data: {
					pid: this.data.pid,
					nums: this.data.nums,
					wh_id:this.data.wh_id,
					from: this.data.goodsType
				},
				method: 'get'
			}, res => {
				res.data.cashier_decca = this.get_thumb(res.cashier_decca, 'wide');
			let that = this;
			wx.navigateTo({
				// url: '/pages/order_confirm/order_confirm',
				url: '/pages/neworder_confirm/order_confirm',
				success: function (obj) {
					// 通过eventChannel向被打开页面传送数据

					obj.eventChannel.emit('orderData', {
						wh_id: that.data.wh_id,
						// index: that.data.areaData[that.data.tabIndex].index,
						data: res.data,
						m_addr: res.m_addr,
						activeCar: that.data.whType=="pindan"?that.data.whType:that.data.goodsType,
						wuliu: res.data.freight_list,
						specialList: res.data.extra_list,
						pack_list: res.data.pack_list,
						buyType:1,
						order_pei_type:res.order_pei_type,
                        orderWarehouse:res.warehouse
					})
				}
			})
			});
		}
	},
	nums_change(e) {
		// this.data.nums = e.detail;
		this.setData({
			nums: e.detail
		})
		// console.log("nums_change", e)
	},
	onClickIcon() {

		wx.switchTab({
			url: '/pages/cls/index/index',
		})
	},
	toStore() {
		wx.navigateTo({
			url: '/pages/store/storeInfo/storeInfo?supplier_id=' + this.data.supplier_id
		})
	},
	onShareAppMessage(res) {
		let path = '/pages/goodsDetail/goodsDetail?pid=' + this.data.pid + '&supplier_id=' + this.data.supplier_id + '&uid=' + getApp().globalData.userInfo.uid+"&from="+this.data.goodsType+"&whType="+this.data.whType+"&wh_id="+this.data.wh_id;
		let title = "您的朋友向你推荐了一款鲜花";
		console.log("path",path)
		return {
			title: title,
			path: path,
			// promise 
		}
	},
})